/*
 * Platform Sample, by Pauladr
 * Adapted from Spritetest by Chaosteil
 * A simplistic little platform game, with a menu, Characters, platforms,
 * a Background, music and sound effects
 */

#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>

#include <wiisprite.h> // The main libwiisprite header.

#include "myulogo.h" // Image data for the Myu logo
#include "generic_point.h" // Image data for the pointer
#include "MenuSprites.h" // Image data for the Menu sprite sheet
#include "menubk.h" // Image data for the menu background

using namespace wsp; // The Namespace used by Libwiisprite

/* Variable Definitions */

GameWindow gwd; // Initializes and renders our scene.

Image pointerimg; // The Pointer Image
Sprite pointer; // The Pointer Sprite itself

Image menubackgroundimg; // The menu background image
Sprite menubackground; // The menu background sprite

Image menuSS; // The Menu sprite sheet
// The sprites for the various buttons, derived from this sheet


/* Function Declarations */
// The Myu games logo screen at the start
void introsprawl();

// ------------------------------------
// Main Program
// ------------------------------------

int main(int argc, char **argv) {
	// Initialize Video
	gwd.InitVideo();

	// Initialise Wiimotes
	WPAD_Init();
	WPAD_SetDataFormat(WPAD_CHAN_ALL, WPAD_FMT_BTNS_ACC_IR);
	
	
	// Open the Intro Sprawl
	introsprawl();

	/* We initialize the layermanager with max. 10 layers*/
	LayerManager manager(10);

	// Load our images -
	if(pointerimg.LoadImage(generic_point) != IMG_LOAD_ERROR_NONE)exit(0);
	if(menubackgroundimg.LoadImage(menubk) != IMG_LOAD_ERROR_NONE)exit(0);
	if(menuSS.LoadImage(MenuSprites) != IMG_LOAD_ERROR_NONE)exit(0);

	// We assign the loaded images to the sprites
	pointer.SetImage(&pointerimg);
	pointer.SetPosition(0, 0);

	menubackground.SetImage(&menubackgroundimg);
	menubackground.SetRefPixelPositioning(REFPIXEL_POS_PIXEL); 
	menubackground.SetPosition(320, 240); //Draws the center at the center of the screen

	TiledLayer newbtn = TiledLayer(1,1,1);
	newbtn.SetStaticTileset(&menuSS, 120, 60);
	newbtn.SetCell(0,0,1);
	newbtn.SetPosition(40,320);

	TiledLayer loadbtn = TiledLayer(1,1,1);
	loadbtn.SetStaticTileset (&menuSS, 120, 60);
	loadbtn.SetCell(0,0,3);
	loadbtn.SetPosition(180,320);

	TiledLayer optsbtn = TiledLayer(1,1,1);
	optsbtn.SetStaticTileset (&menuSS, 120, 60);
	optsbtn.SetCell(0,0,5);
	optsbtn.SetPosition(320,320);

	TiledLayer exitbtn = TiledLayer(1,1,1);
	exitbtn.SetStaticTileset (&menuSS, 120, 60);
	exitbtn.SetCell(0,0,7);
	exitbtn.SetPosition(460,320);

	// Load the sprites into the LayerManager

	manager.Append(&pointer); // Gets drawn the closest.
	manager.Append(&newbtn);
	manager.Append(&loadbtn);
	manager.Append(&optsbtn);
	manager.Append(&exitbtn);
	manager.Append(&menubackground); // Gets drawn the farthest.
	
	int newcoll = 0;
	while(1) {
		// Call WPAD_ScanPads each loop, this reads the latest controller states
		WPAD_ScanPads();

		// WPAD_ButtonsDown tells us which buttons were pressed in this loop
		// this is a "one shot" state which will not fire again until the button has been released
		u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);

		// We exit if home is pressed
		if(pressed & WPAD_BUTTON_HOME) break;

		// Infrared calculation
		ir_t ir; // The struct for infrared
		WPAD_IR(WPAD_CHAN_0, &ir); // Let's get our infrared data
		// Give our sprite the positions and the angle.
		pointer.SetPosition(ir.sx-WSP_POINTER_CORRECTION_X, ir.sy-WSP_POINTER_CORRECTION_Y); // We use these constants to translate the position correctly to the screen
		pointer.Move(-((f32)pointer.GetWidth()/2), -((f32)pointer.GetHeight()/2)); // And these to make our image appear at the center of this position.
		pointer.SetRotation(ir.angle/2); // Set angle/2 to translate correctly
		bool newcollb = pointer.CollidesWith(&loadbtn);
		if (newcoll == 0 && newcollb) {
			loadbtn.SetCell(0,0,4);
			newcoll = 1;
			
		} else if (newcoll == 1 && !newcollb) {
			loadbtn.SetCell(0,0,3);
			newcoll = 0;
		}
		// Draw everything what's in the manager.
		manager.Draw(0, 0); 

		// Finish rendering of everything.
		gwd.Flush();
	}

	// One last draw and flush..
	manager.Draw(0, 0);
	gwd.Flush();
	
	// And we return to our loader!
	return 0;
}


// -------------------------------------
// Introsprawl : Displays the opening Logo
// -------------------------------------

void introsprawl() {
	//"Fade in"
	Sprite introSprite;
	Image* introImg = new Image();
	if(introImg->LoadImage(myulogo, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)
		exit(0);
	
	introSprite.SetImage(introImg);
	introSprite.SetRefPixelPositioning(REFPIXEL_POS_PIXEL);
	introSprite.SetPosition(320,240);
	
	introSprite.SetTransparency(0);
	gwd.Flush();
	
	for(int i = 0; i < 255; i += 10) //255 *2
	{
		introSprite.SetTransparency(i);
		introSprite.Draw();
		gwd.Flush();
	}
	while (1) {
		WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
		// We start the exiting via setting the fading value to 2.
		if(pressed & WPAD_BUTTON_A) break;
		if(pressed & WPAD_BUTTON_HOME) return;
	}
		for(int i = 250; i > 0; i -= 10)
	{
		introSprite.SetTransparency(i);
		introSprite.Draw();
		gwd.Flush();
	}
}
